<html>
<head><meta charset="utf-8"><title>Per request scratch spaces (via bumpallo) · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html">Per request scratch spaces (via bumpallo)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236154071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236154071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236154071">(Apr 26 2021 at 11:00)</a>:</h4>
<p>Discussion thread for <a href="https://github.com/rust-analyzer/rust-analyzer/issues/8663">https://github.com/rust-analyzer/rust-analyzer/issues/8663</a></p>



<a name="236157994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236157994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236157994">(Apr 26 2021 at 11:36)</a>:</h4>
<p>what kind of thing would get put into the scratch space? I'd think the biggest parts of the work happen behind salsa queries. Maybe that's not true for things like highlighting</p>



<a name="236158346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236158346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236158346">(Apr 26 2021 at 11:39)</a>:</h4>
<p>Nothing in particular, but it feels like we might be doing some allocs:</p>
<p>λ rg collect crates/ide/ | wc -l<br>
40</p>



<a name="236158391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236158391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236158391">(Apr 26 2021 at 11:39)</a>:</h4>
<p>assists often allocate a buch for the final edit</p>



<a name="236158588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236158588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236158588">(Apr 26 2021 at 11:41)</a>:</h4>
<p>but yeah, threading this to the salsa would be nice. Although it's unclear how to best to that</p>



<a name="236158636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236158636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236158636">(Apr 26 2021 at 11:41)</a>:</h4>
<p>a naive apporach is to use one scratch space for the whole end-to-end query, but I fear that that might be too much</p>



<a name="236158718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236158718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236158718">(Apr 26 2021 at 11:42)</a>:</h4>
<p>like, the root query might be "typecheck the world", and, in that situation, not freeing anything at all until the very end seems to be excessive.</p>



<a name="236159358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236159358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236159358">(Apr 26 2021 at 11:48)</a>:</h4>
<p>I guess, in an ideal world this should look like this:</p>
<ul>
<li>when invoking salsa, the caller can provide a  stack allocator -- a bump allocator, which also track frames obeying as tack disciplin</li>
<li>query impl function get access to the this stack allocator. They can allocate temporary objects into it but (unlike scratch space in the issue), they can not return data stored in this alloc</li>
<li>when executing a query, salsa runtime pushes a new frame on the stack allocator, and then pops the whole frame once the query fn finishes. </li>
</ul>
<p>In this scheme, unlike "a scratch space for top-level query", we only keep scrats space for the stack of active queries, and that's bounded</p>



<a name="236159603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236159603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236159603">(Apr 26 2021 at 11:50)</a>:</h4>
<p>So, eg, this Vec looks like it could ideally be allocated onto some arena: <a href="https://github.com/matklad/rust-analyzer/blob/7bb9c147c0e4815ff8ed48dfee1d8267133bbef7/crates/hir_ty/src/method_resolution.rs#L904">https://github.com/matklad/rust-analyzer/blob/7bb9c147c0e4815ff8ed48dfee1d8267133bbef7/crates/hir_ty/src/method_resolution.rs#L904</a></p>



<a name="236159654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236159654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236159654">(Apr 26 2021 at 11:51)</a>:</h4>
<p>Although maybe what we need here is just <code>alloca</code> and huge stacks?</p>



<a name="236159726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236159726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236159726">(Apr 26 2021 at 11:51)</a>:</h4>
<p>I was about to ask, why not just create a new arena owned by the query function? (or maybe those arenas could be pooled)</p>



<a name="236160063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236160063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236160063">(Apr 26 2021 at 11:55)</a>:</h4>
<p>Oh, does this show up in profiles?</p>



<a name="236160934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236160934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236160934">(Apr 26 2021 at 12:01)</a>:</h4>
<p>Yeah, that's a better way to do that :)</p>



<a name="236160953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236160953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236160953">(Apr 26 2021 at 12:01)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink  [he/him]</span> this = allocations?</p>



<a name="236160964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236160964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236160964">(Apr 26 2021 at 12:01)</a>:</h4>
<p>yeah</p>



<a name="236160977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236160977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236160977">(Apr 26 2021 at 12:02)</a>:</h4>
<p>Not really, and that's why I am conflicted here</p>



<a name="236161096"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236161096" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236161096">(Apr 26 2021 at 12:02)</a>:</h4>
<p>Doing per-request arena is obviously the right way to approach this on the one hand, and it has significant architectural implications (retro-fitting arenas is painful).</p>



<a name="236161182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236161182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236161182">(Apr 26 2021 at 12:03)</a>:</h4>
<p>But it is probably not the biggest perf sink at the moment for us</p>



<a name="236203583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236203583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236203583">(Apr 26 2021 at 16:25)</a>:</h4>
<p>I wonder if you could hide this with TLS or something for each request.</p>



<a name="236204391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Per%20request%20scratch%20spaces%20%28via%20bumpallo%29/near/236204391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Per.20request.20scratch.20spaces.20(via.20bumpallo).html#236204391">(Apr 26 2021 at 16:30)</a>:</h4>
<p>No borrows of anything inside of a thread local can escape the <code>MY_TLS_VAR.with(|my_tls_var| /*...*/)</code> closure without unsafe code. That includes anything allocated inside the arena. I think it won't be easy to make the unsafe code sound.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>